require('es6-promise').polyfill();

import axios from 'axios'

function loadWorkOrders(data) {
  return {
    type: 'LOAD_WORK_ORDERS',
    data
  }
}

function loadWorkOrderLines(data) {
  return {
    type: 'LOAD_WORK_ORDER_LINES',
    data
  }
}

function loadApplyingWorkOrder(data) {
  return {
    type: 'LOAD_APPLYING_WORK_ORDER_COUNT',
    data
  }
}

export function loadWorkOrdersAsync(data) {
  return function(dispatch) {
    return axios.post('/api/work/query', data)
      .then((resp)=> {
        dispatch(loadWorkOrders(resp.data))
      })
      .catch((error) => {
        alert(error.data.errMsg)
      })
  }
}

export function loadWorkOrderLinesAsync(data) {
  return function(dispatch) {
    return axios.post('/api/work/queryLines', data)
      .then((resp)=> {
        dispatch(loadWorkOrderLines(resp.data))
      })
      .catch((error) => {
        alert(error.data.errMsg)
      })
  }
}

export function createWorkOrderAsync(data) {
  return function(dispatch) {
    return axios.post('/api/work/sysCreate', data)
      .then((resp)=> {
        dispatch(loadWorkOrdersAsync({}))
      })
      .catch((error) => {
        console.error(error)
        alert(error.data.errMsg)
      })
  }
}

export function addOrderLineAsync(data) {
  return function(dispatch) {
    return axios.post('/api/work/addOrderLine', data)
      .then((resp)=> {
        dispatch(loadWorkOrderLinesAsync(data))
        dispatch(loadWorkOrdersAsync(data))
      })
      .catch((error) => {
        alert(error.data.errMsg)
      })
  }
}


export function generateAsync(data) {
  return function(dispatch) {
    return axios.post('/api/work/generate', data)
      .then((resp)=> {
        dispatch(loadWorkOrdersAsync(data))
        dispatch(loadApplyingWorkOrderAsync({}))
      })
      .catch((error) => {
        console.error(error)
        alert(error.data.errMsg)
      })
  }
}

export function pushJdeAsync(data) {
  return function(dispatch) {
    return axios.post('/api/work/pushCompleteToJde', data)
      .then((resp)=> {
        dispatch(loadWorkOrdersAsync(data))
        dispatch(loadApplyingWorkOrderAsync({}))
      })
      .catch((error) => {
        console.error(error)
        alert(error.data.errMsg)
      })
  }
}

export function updateCompleteAsync(data) {
  return function(dispatch) {
    return axios.post('/api/work/updateComplete', data)
      .then((resp)=> {
        dispatch(loadWorkOrdersAsync(data))

      })
      .catch((error) => {
        alert(error.data.errMsg)
      })
  }
}


export function deleteWorkOrderAsync(data) {
  return function(dispatch) {
    return axios.post('/api/work/delete', data)
      .then((resp)=> {
        dispatch(loadWorkOrdersAsync(data))
      })
      .catch((error) => {
        alert(error.data.errMsg)
      })
  }
}

export function cancelWorkOrderAsync(data) {
  return function(dispatch) {
    return axios.post('/api/work/cancel', data)
      .then((resp)=> {
        dispatch(loadWorkOrdersAsync(data))
      })
      .catch((error) => {
        alert(error.data.errMsg)
      })
  }
}

export function loadApplyingWorkOrderAsync(data) {
  return function(dispatch) {
    return axios.post('/api/work/queryApplingCount', data)
      .then((resp)=> {
        dispatch(loadApplyingWorkOrder(resp.data))
      })
      .catch((error) => {

      })
  }
}

export function payOfflineWorkOrderAsync(data) {
  return function(dispatch) {
    return axios.post('/api/work/pay_offline', data)
      .then((resp)=> {
        dispatch(loadWorkOrdersAsync(data))
      })
      .catch((error) => {
        alert(error.data.errMsg)
      })
  }
}

export function deleteWorkOrderLineAsync(data) {
  return function(dispatch) {
    return axios.post('/api/work/deleteLine', data)
      .then((resp)=> {
        dispatch(loadWorkOrderLinesAsync(data))
        dispatch(loadWorkOrdersAsync(data))
      })
      .catch((error) => {
        alert(error.data.errMsg)
      })
  }
}

export function printWorkOrderAsync(data) {
  return function(dispatch) {
    return axios.get('/api/work/print?id='+data.id)
      .then((resp)=> {
        // dispatch(loadApplyingWorkOrder(resp.data))
      })
      .catch((error) => {

      })
  }
}
